using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using WPCoco;

namespace CocoCube
{
    public class Model3D : Node3D
    {
        public Model3D(Model model)
        {
            this.model = model;
        }

        Model model;

        public Model Model
        {
            get { return model; }
            private set { model = value; }
        }

        float ry = 0;

        public override void SelfDraw(GameTime gameTime)
        {
            if (model == null) return;
            Matrix m = Matrix.CreateRotationY(ry);
            
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                ModelMesh mesh = model.Meshes[i];
                for (int j = 0; j < mesh.Effects.Count; j++)
                {
                    BasicEffect effect = mesh.Effects[j] as BasicEffect;
                    if (effect == null) continue;
                    effect.World = m;
                    effect.View = WorldMatrix.Shared.CurrentMatrix * Camera.WorldViewMatrix;
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(1, ParentLayer.Width / ParentLayer.Height, 1, 10000.0f);
                    //effect.Projection = Matrix.CreatePerspective(ParentLayer.Width, ParentLayer.Height, 1, 10000.0f);
                    //effect.Projection = Matrix.CreatePerspectiveOffCenter(0, ParentLayer.Width, 0, ParentLayer.Height, 1, 10000);
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    
                }
                mesh.Draw();
            }
            //model.Draw(m, Camera.WorldViewMatrix, Matrix.CreatePerspective(ParentLayer.Width, ParentLayer.Height, 10, 1000));
            ry += 0.01f;
        }
    }
}
